package com.ruoyi.notice.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.notice.domain.SysNotices;
import com.ruoyi.notice.service.SysNoticesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YL
 * @version 1.0
 * @date 2020/6/29 14:08
 */
@RestController
@RequestMapping("notice")
public class SysNoticesController extends BaseController {

    @Autowired
    private SysNoticesService noticeService;

    /**
     * 查询所有普通公告号列表
     */
    @GetMapping("all")
    public List<SysNotices> selectAll(){
        return noticeService.selectAll();
    }

    /**
     * 查询所有寻源公告号列表
     */
    @GetMapping("all2")
    public List<SysNotices> selectAll2(){
        return noticeService.selectAll2();
    }

    /**
     * 根据id查询公告信息
     */
    @GetMapping("get/{id}")
    public SysNotices get(@PathVariable("id") Long id) {
        return noticeService.selectNoticeById(id);

    }

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("list")
    public R list(SysNotices notice) {
        startPage();
        return result(noticeService.selectNoticeList(notice));
    }


    /**
     * 新增保存【请填写功能名称】
     */
    @PostMapping("save")
    public R addSave(@RequestBody SysNotices notice) {
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @PostMapping("update")
    public R editSave(@RequestBody SysNotices notice) {
        int row=noticeService.updateNotice(notice);
        if (row==-1){
            return R.error(400,"禁止修改已发布公告！");
        }
        return toAjax(row);
    }

    /**
     * 终止公告
     */
    @PostMapping("break")
    public R breakNotice(@RequestBody SysNotices notice) {
        return toAjax(noticeService.breakNotice(notice));
    }

    /**
     * 删除${tableComment}
     */
    @PostMapping("removes")
    public R remove(String ids) {
        return toAjax(noticeService.deleteNoticeByIds(ids));
    }

    @PostMapping("remove")
    public R remove(Long id) {
        int row=noticeService.deleteNoticeById(id);
        if (row==-1){
            return R.error(400,"禁止删除已发布公告！");
        }
        return toAjax(row);
    }

    @GetMapping("select/{noticeId}")
    public List<String> select(@PathVariable("noticeId")Long noticeId) {
        return noticeService.selectMaterialByNoticeId(noticeId);
    }

    @PostMapping("edit")
    public R edit(@RequestBody SysNotices notice) {
        noticeService.deleteMaterial(notice.getNoticeId());
        return toAjax(noticeService.insertMaterial(notice));
    }

    @PostMapping("delete/{noticeId}")
    public R delete(@PathVariable("noticeId")Long noticeId) {
        return toAjax(noticeService.deleteMaterial(noticeId));
    }
}
